package olSystem;

import com.spm.utils.StudentMapperUtil;
import com.spm.utils.TRCMapperUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 * Servlet implementation class RemainAddServlet
 */
@WebServlet("/RemainAddServlet")
public class RemainAddServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {

        process(req, resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        process(req, resp);
    }

    protected void process(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
//        try {
//            DBOper db = new DBOper();
//            Connection conn = db.getConnection();
//            Statement statement = (Statement) conn.createStatement();

//            String keyString = new String(req.getParameter("id").getBytes("iso-8859-1"), "utf-8");

            req.setCharacterEncoding("UTF-8");
            //课程号
            String keyString = req.getParameter("id");
            String tid,cno;
            if(keyString == null) {
                keyString = "";
                resp.sendRedirect("FindServlet");

                return;
            }
            else {
                String[] k = keyString.split("\\+");
                cno = k[0];
                tid = k[1];
                System.out.println("cno="+cno);
                System.out.println("tid="+tid);
            }
            //添加课程,只能选一门课程
            String numSession = req.getSession().getAttribute("numSession").toString();
//            String sql1 = "select Cno from usst.student_r_course where sno = " + numSession;
//            String sql11 = "select * from usst.course where Cno = " + sql1;
//            String sql1 = "select Sscourse from student where Sno = " + numSession;
//            ResultSet resultSet = statement.executeQuery(sql1);
//            String course = "";
//            while (resultSet.next()) {
//                course = resultSet.getString("Sscourse");
//            }
//            String sql11 = "select Cname from course where Cno = " + keyString;
//            ResultSet resultSet1 = statement.executeQuery(sql11);
//            String cn="";
//            while (resultSet1.next()) {
//                cn = resultSet1.getString("Cname");
//            }
            System.out.println("===========================RAS======================");
            System.out.println("tid="+tid+"cno="+cno+"sno="+numSession);
            StudentMapperUtil studentMapperUtil =new StudentMapperUtil();
            TRCMapperUtil trcMapperUtil = new TRCMapperUtil();
            trcMapperUtil.MinusOneSurplus(tid,cno);
            studentMapperUtil.insertSelectedCourse(tid,numSession,cno);
            //还没有选课
//            if(course==null||course.length()==0) {
//
//                //修改课程数据
//                String sql2 = "update student set Sscourse=\'" + cn+ "\' where Sno = " + numSession;
//                statement.executeUpdate(sql2);
//
//                //课程余量-1
//                String sql = "update course set Capacity=Capacity-1 where Cno = \'"+ keyString + "\'";
//                statement.executeUpdate(sql);
//
//            }else {
//                PrintWriter pw = resp.getWriter();
//                pw.println("<h4>重复选课</h4>");
//            }


//            statement.close();
//            conn.close();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }

        resp.getWriter().print("{\"data\":\"返回json数据！\"}");
    }

}
